package cn.iocoder.yudao.module.contract.controller.admin.basicData;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicProductCodeDO;
import cn.iocoder.yudao.module.contract.service.basicData.BasicProductCodeService;

@Tag(name = "管理后台 - 产品编码")
@RestController
@RequestMapping("/contract/basic-product-code")
@Validated
public class BasicProductCodeController {

    @Resource
    private BasicProductCodeService basicProductCodeService;

    @PostMapping("/create")
    @Operation(summary = "创建产品编码")
    @PreAuthorize("@ss.hasPermission('contract:basic-product-code:create')")
    public CommonResult<Long> createBasicProductCode(@Valid @RequestBody BasicProductCodeSaveReqVO createReqVO) {
        return success(basicProductCodeService.createBasicProductCode(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品编码")
    @PreAuthorize("@ss.hasPermission('contract:basic-product-code:update')")
    public CommonResult<Boolean> updateBasicProductCode(@Valid @RequestBody BasicProductCodeSaveReqVO updateReqVO) {
        basicProductCodeService.updateBasicProductCode(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品编码")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('contract:basic-product-code:delete')")
    public CommonResult<Boolean> deleteBasicProductCode(@RequestParam("id") Long id) {
        basicProductCodeService.deleteBasicProductCode(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品编码")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('contract:basic-product-code:query')")
    public CommonResult<BasicProductCodeRespVO> getBasicProductCode(@RequestParam("id") Long id) {
        BasicProductCodeDO basicProductCode = basicProductCodeService.getBasicProductCode(id);
        return success(BeanUtils.toBean(basicProductCode, BasicProductCodeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品编码分页")
    @PreAuthorize("@ss.hasPermission('contract:basic-product-code:query')")
    public CommonResult<PageResult<BasicProductCodeRespVO>> getBasicProductCodePage(@Valid BasicProductCodePageReqVO pageReqVO) {
        PageResult<BasicProductCodeDO> pageResult = basicProductCodeService.getBasicProductCodePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BasicProductCodeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品编码 Excel")
    @PreAuthorize("@ss.hasPermission('contract:basic-product-code:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBasicProductCodeExcel(@Valid BasicProductCodePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BasicProductCodeDO> list = basicProductCodeService.getBasicProductCodePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "产品编码.xls", "数据", BasicProductCodeRespVO.class,
                        BeanUtils.toBean(list, BasicProductCodeRespVO.class));
    }

}